package tech.mhuang.interchan.sso;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.mhuang.interchan.sso.sysuser.service.ISysUserService;
import tech.mhuang.pacebox.springboot.autoconfiguration.datasecure.annation.DataSecureField;
import tech.mhuang.pacebox.springboot.autoconfiguration.datasecure.annation.DecryptMapping;
import tech.mhuang.pacebox.springboot.autoconfiguration.datasecure.annation.EncryptMapping;
import tech.mhuang.pacebox.springboot.autoconfiguration.datasecure.consts.DecryptType;
import tech.mhuang.pacebox.springboot.autoconfiguration.datasecure.consts.EncryptType;
import tech.mhuang.pacebox.springboot.core.entity.RequestModel;
import tech.mhuang.pacebox.springboot.core.rest.SingleRestTemplate;
import tech.mhuang.pacebox.springboot.protocol.Result;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private ISysUserService sysUserService;

    @EncryptMapping
    @GetMapping("/encrypt")
    public Result encrypt() {
        return Result.ok("123456");
    }

    @DecryptMapping
    @PostMapping("/decrypt")
    public Result decrypt(@RequestBody Result result) {
        return result;
    }

    @EncryptMapping("pacebox")
    @GetMapping("/pacebox/encrypt")
    public Result paceboxAjax() {
        return Result.ok("123456");
    }

    @DecryptMapping("pacebox")
    @PostMapping("/pacebox/decrypt")
    public Result paceboxDecrypt(@RequestBody Result result) {
        return result;
    }

    @EncryptMapping(type = EncryptType.FIELD)
    @DecryptMapping(type = DecryptType.FIELD)
    @PostMapping("/field/decrypt")
    public T1 fieldDecrypt(@RequestBody T1 t) {
        System.out.println(t.getId());
        return t;
    }

    @Autowired
    private SingleRestTemplate singleRestTemplate;

    @GetMapping("/jaegerTestMethod")
    public Result jaegerTestMethod(String id){
        RequestModel<String> requestModel = new RequestModel<String>() {};
        requestModel.setMethod(HttpMethod.GET);
        requestModel.setUrl("http://127.0.0.1/open");
        ResponseEntity<String> result = singleRestTemplate.request(requestModel);
        if(result.getStatusCode() == HttpStatus.OK){
            return Result.ok(result.getBody());
        }
        return Result.ok(id);
    }
    @GetMapping("/jaeger/test")
    public Result jaegerTest(String id){
        sysUserService.getById("1");
//        ResponseEntity<Result> result = restTemplate.getForEntity("http://127.0.0.1:8084/test/jaegerTestMethod",Result.class);
        RequestModel<Result> requestModel = new RequestModel<Result>() {};
        requestModel.setMethod(HttpMethod.GET);
        requestModel.setUrl("http://127.0.0.1:8084/test/jaegerTestMethod?id="+id);
        ResponseEntity<Result> result = singleRestTemplate.request(requestModel);
        if(result.getStatusCode() == HttpStatus.OK){
            RequestModel<Result> requestModel2 = new RequestModel<Result>() {};
            requestModel2.setUrl("http://127.0.0.1:8084/test/jaeger/post");
            requestModel2.setParams(result.getBody());
            result = singleRestTemplate.request(requestModel2);
            if(result.getStatusCode() == HttpStatus.OK){
                sysUserService.queryAll();
                return result.getBody();
            }
            return Result.faild("错误状态码"+result.getStatusCodeValue()+"");
        }

        return Result.faild("错误状态码"+result.getStatusCodeValue()+"");
    }
    @PostMapping("/jaeger/post")
    public Result post(@RequestBody  Result result){
        return result;
    }

    @Data
    static class T1 {
        @DataSecureField
        private String id;

    }
}
